package controllers;

import java.util.ArrayList;

import model.forview.AllTypesModelForView;
import model.forview.TypeCreateModelForView;
import model.forview.TypeDeleteModelForView;
import model.forview.TypeModelForView;
import model.wrap.type.Type;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.typeCreate;
import views.html.allTypes;
import views.html.gettype;

public class Types extends Controller
{

    public static Result getAllTypes()
    {
	ArrayList<Type> types = Type.getAllTypes();
	
	AllTypesModelForView model = new AllTypesModelForView(types);
	return ok(allTypes.render(model));
    }

    public static Result getType(Long typeId)
    {
	Type type = new Type(typeId);
	TypeModelForView forView = new TypeModelForView(type);
	return ok(gettype.render(forView));
    }

    public static Result getPageTypeCreate()
    {
	TypeCreateModelForView model = new TypeCreateModelForView();
	return ok(typeCreate.render(model));
    }

    public static Result typeCreate()
    {
	Form<TypeCreateModelForView> forModel = form(
		TypeCreateModelForView.class).bindFromRequest();
	Type type = new Type(forModel.get());
	type.save();

	return redirect(routes.Types.getAllTypes());
    }

    public static Result getPageTypeDelete(Long idType)
    {
	TypeDeleteModelForView modelForView = new TypeDeleteModelForView(new Type(idType));
	return ok(views.html.typeDelete.render(modelForView));
    }

    public static Result typeDelete()
    {
	TypeDeleteModelForView modelForView = new TypeDeleteModelForView(form().bindFromRequest().data());
	Type type = modelForView.getType();
	try
	{
	    type.delete();
	}
	catch (Exception e)
	{
	    // TODO добавить вывод эксепшена когда будет готов flash
	    return redirect(routes.Types.getAllTypes());
	}
	return redirect(routes.Types.getAllTypes());
    }

    public static Result getPageTypeChange()
    {
	return null; // ok(index.render("Your new application is ready."));
    }

    public static Result typeChange()
    {
	return null;// ok(index.render("Your new application is ready."));
    }
}
